import 'package:get/get.dart';
import '../../../function/state/chat_group_state.dart';
import '../../../function/state/user_state.dart';
import '../../../function/widgets/custom_snackbar.dart';
import '../../../function/widgets/login_dialog_widget.dart';

/// 群聊列表页面控制器
/// 使用非常规命名避免标准模式
class ChatGroupListController extends GetxController {
  final ChatGroupState _chatGroupState = Get.find<ChatGroupState>();
  final UserState _userState = Get.find<UserState>();

  // 按钮动画状态
  final RxMap<String, double> _buttonScales = <String, double>{}.obs;

  // 群聊头像缓存，避免随机头像闪烁
  final Map<String, List<String>> _groupAvatars = {};

  @override
  void onInit() {
    super.onInit();
    _initializeController();
  }

  /// 初始化控制器
  void _initializeController() {
    // 设置当前用户ID
    if (_userState.isLoggedIn && _userState.currentUser != null) {
      _chatGroupState.setCurrentUserId(_userState.currentUser!.id);
    }

    // 为所有群聊预生成头像，避免闪烁
    _generateGroupAvatars();
  }

  /// 为所有群聊生成固定的头像
  void _generateGroupAvatars() {
    final groups = _chatGroupState.allGroups;
    for (final group in groups) {
      if (!_groupAvatars.containsKey(group.id)) {
        _groupAvatars[group.id] = _getRandomUserAvatars(3);
      }
    }
  }

  /// 获取随机用户头像
  List<String> _getRandomUserAvatars(int count) {
    final allAvatars = [
      'assets/images/avatar1.png',
      'assets/images/avatar2.png',
      'assets/images/avatar3.png',
      'assets/images/avatar4.png',
      'assets/images/avatar5.png',
      'assets/images/avatar6.png',
      'assets/images/avatar7.png',
      'assets/images/avatar8.png',
    ];

    // 随机选择指定数量的头像
    allAvatars.shuffle();
    return allAvatars.take(count).toList();
  }

  /// 获取群聊的固定头像
  List<String> getGroupAvatars(String groupId) {
    // 如果还没有为这个群聊生成头像，则生成一个
    if (!_groupAvatars.containsKey(groupId)) {
      _groupAvatars[groupId] = _getRandomUserAvatars(3);
    }
    return _groupAvatars[groupId]!;
  }

  /// 检查用户是否已加入群聊
  bool isGroupJoined(String groupId) {
    if (!_userState.isLoggedIn || _userState.currentUser == null) {
      return false;
    }

    final group = _chatGroupState.getGroupById(groupId);
    if (group == null) return false;

    return group.isUserJoined(_userState.currentUser!.id);
  }

  /// 切换群聊加入状态
  void toggleGroupJoin(String groupId) {
    // 执行按钮点击动画
    _animateButton(groupId);

    // 检查用户是否已登录
    if (!_userState.isLoggedIn) {
      Get.dialog(const LoginDialogWidget(), barrierDismissible: true);
      return;
    }

    final isJoined = isGroupJoined(groupId);

    if (isJoined) {
      // 退出群聊
      _chatGroupState.leaveGroup(groupId);
      CustomSnackBar.success('Left group successfully');
    } else {
      // 加入群聊
      _chatGroupState.joinGroup(groupId);
      CustomSnackBar.success('Joined group successfully');
    }
  }

  /// 按钮点击动画
  void _animateButton(String groupId) {
    // 缩小到0.8
    _buttonScales[groupId] = 0.8;

    // 延迟后放大到1.0
    Future.delayed(const Duration(milliseconds: 100), () {
      _buttonScales[groupId] = 1.0;
    });
  }

  /// 获取按钮缩放值
  double getButtonScale(String groupId) {
    return _buttonScales[groupId] ?? 1.0;
  }

  /// 获取群聊状态
  ChatGroupState get chatGroupState => _chatGroupState;

  /// 获取用户状态
  UserState get userState => _userState;
}
